package dao

import (
	"encoding/json"
	"fmt"
	database "hft/Database"
	"hft/Database/admin"
	"hft/global"
	"hft/test"
	"hft/test/mi/loupan/loupan_utils"
	"hft/test/mi/loupan/model"
	"hft/test/mi/tool"
	"hft/tools"
	"strconv"
	"strings"
	"time"
)

// 解析楼盘
func ParseLou() {
	var list = make([]*model.Lou, 0)
	var regions = make([]*admin.Region, 0)
	var properties = make([]*admin.Property, 0)
	var tags = make([]*admin.CommonTag, 0)
	var tese = make([]*admin.LouPanTeSe, 0)
	var roomsList = make([]*admin.RoomType, 0)
	var los = make([]*admin.LouPan, 0)
	global.OC_DB.Find(&roomsList)
	global.OC_DB.Find(&tese)
	global.OC_DB.Find(&tags)
	global.OC_DB.Find(&properties)
	global.OC_DB.Where("p_id = ?", 72).Find(&regions)
	test.Sql().Table("flw_propre").Where("city = ?", "中山市").Find(&list)
	for _, v := range list {
		jsonStr, _ := tool.ConvertToJSON(v.Lxq)
		p := model.Lxq{}
		json.Unmarshal([]byte(jsonStr), &p)
		// 找出区域
		region := loupan_utils.FindRegion(regions, v.Area)
		// 经纬度
		lng, lat := loupan_utils.GetLatLng(v.Zb)
		// 面积
		area := loupan_utils.ExtractNumbers(v.Lreal)
		// 占地面积
		floorArea := loupan_utils.ExtractNumber(p.FloorArea)
		// 建筑面积
		buildArea := loupan_utils.ExtractNumber(p.BuildArea)
		// 建筑栋数
		buildNum := loupan_utils.ExtractNumber(p.BuildNum)
		// 容积率
		volume := loupan_utils.ExtractNumber(p.VolumeRate)
		// 绿化率
		green := loupan_utils.ExtractNumber(p.GreeningRate)
		// 获取物业用途
		pros := loupan_utils.GetProperties(v.Zcate, properties)
		// 标签
		nowTags := loupan_utils.GetTags(v.Labels, tags)
		// 特色
		teseList := loupan_utils.GetTese(v.Tcate, tese, v.Bs)
		// 户型
		rooms := loupan_utils.GetRoomTypes(v.Hcate, roomsList)
		// 获取
		var louItem = &admin.LouPan{
			Base:           database.Base{ID: uint(v.ID), Sort: v.Sort},
			Name:           v.Lname,
			Cover:          v.Fpic,
			RegionLink:     fmt.Sprintf("%v%v,", region.Link, region.ID),
			BelongName:     v.Area,
			FirstWord:      tools.PinYin(v.Lname),
			ReferencePrice: v.Price,
			Longitude:      lng,
			Latitude:       lat,
			MinArea:        area[0],
			MaxArea:        area[1],
			Years:          p.Ownership,
			Grounding:      1,
			Address:        v.Address,
			Furnish:        p.Decoration,
			Status:         v.Xcate,
			LouPanArea: admin.LouPanArea{
				LouPanId:        v.ID,
				FloorArea:       float32(floorArea),
				BuildArea:       float32(buildArea),
				BuildNum:        int(buildNum),
				UserNum:         p.UserNum,
				ParkingSpace:    p.ParkingSpace,
				VolumeRate:      float32(volume),
				GreeningRate:    float32(green),
				PropertyCost:    p.PropertyFee,
				PropertyCompany: p.PropertyCo,
			},
			SellLouPanInfo: admin.SellLouPanInfo{
				LouPanId:    v.ID,
				OpenTime:    int(loupan_utils.ExtractTime(v.Kftime)),
				GetRoomTime: int(loupan_utils.ExtractTime(p.Delivery)),
				License:     p.License,
			},
			Properties: pros,
			Tags:       nowTags,
			TeSe:       teseList,
			RoomType:   rooms,
		}
		los = append(los, louItem)
	}
	global.OC_DB.Create(&los)
}

// 更新所属
func UpdateBelong() {
	var lous []*admin.LouPan
	var regions []*admin.Region
	global.OC_DB.Find(&regions)
	global.OC_DB.Find(&lous)
	for _, v := range lous {
		var belongName = loupan_utils.GetBelongName(regions, v.RegionLink)
		global.OC_DB.Model(&admin.LouPan{}).Where("id = ?", v.ID).Update("belong_name", belongName)
	}
}

// 更新区域
func UpdateArea() {
	var list []*admin.LouPan
	global.OC_DB.Find(&list)
	for _, v := range list {
		var strarr = strings.Split(v.RegionLink, ",")
		var region, _ = strconv.Atoi(strarr[len(strarr)-2])
		global.OC_DB.Model(&admin.LouPan{}).Where("id = ?", v.ID).Update("region", region)
	}
}

// 更新动态
func UpdateActive() {
	var list []*admin.LouPan
	global.OC_DB.Find(&list)
	for _, v := range list {
		var active *admin.ActiveBase
		global.OC_DB.Where("lou_pan_id = ?", v.ID).First(&active).Order("update_time desc").Limit(1)
		if (active.UpdateTime != time.Time{}) {
			var t = active.UpdateTime.Unix()
			global.OC_DB.Model(&admin.LouPan{}).Where("id = ?", v.ID).Update("active_last_update", t)
		}
	}
}

// 迁移地址
func UpdateAddress() {

}
